package com.heima.behavior.service.impl;

import com.heima.behavior.service.BehavorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.schedule.CacheService;
import com.heima.model.behavor.dto.LikesBehaviorDto;
import com.heima.model.behavor.dto.ReadBehaviorDto;
import com.heima.model.behavor.dto.UnLikesBehaviorDto;
import com.heima.model.behavor.dto.UpdateArticleMess;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class BehavorServiceImpl implements BehavorService {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    private CacheService cacheService;


    @Override
    public ResponseResult likes(LikesBehaviorDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer id = user.getId();
        if (id==null  || id ==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Short operation = dto.getOperation();
        Long articleId = dto.getArticleId();
        Long currentTimeMillis = System.currentTimeMillis();
        if (operation==0){
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(articleId);
            updateArticleMess.setAdd(1);
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            kafkaTemplate.send("itcast-topic-input",updateArticleMess);
            Object o = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + articleId, id.toString());
            if (o==null){
                cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR+articleId,id.toString(),currentTimeMillis.toString());
            }
        }else {
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(articleId);
            updateArticleMess.setAdd(-1);
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            kafkaTemplate.send("itcast-topic-input",updateArticleMess);
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR+articleId,id.toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer id = user.getId();
        if (id==null  || id ==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Long articleId = dto.getArticleId();
        Integer o = (Integer) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + articleId, id.toString());
        if (o==null){
            o=0;
        }
        Integer result=o+1;
        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR+articleId,id.toString(),result.toString());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult unLikes(UnLikesBehaviorDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer id = user.getId();
        if (id==null  || id ==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Long articleId = dto.getArticleId();
        Short type = dto.getType();
        Long currentTimeMillis= System.currentTimeMillis();
        if (type==0){
            Integer o = (Integer) cacheService.hGet(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId, id.toString());
            if (o==null){
                cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR+articleId,id.toString(),currentTimeMillis.toString());
            }
        }else {
            cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR+articleId,id.toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
